package com.wushi.lenovo.asia5b.app_ui.credit.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.ListPopupWindow;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseMvpFragment;
import com.mvp.base.model.ResultModel;
import com.mvp.ui.view.BaseTextWatcher;
import com.mvp.utils.StringUtils;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.FpxFragmentView;
import com.wushi.lenovo.asia5b.app_di.credit.presenter.FpxFragmentPresenter;
import com.wushi.lenovo.asia5b.app_model.credit.bean.FpxBankBean;
import com.wushi.lenovo.asia5b.app_model.credit.bean.ThirdTopupType;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.other.WebUrlType;
import com.wushi.lenovo.asia5b.app_ui.h5.TopupThirdActivity;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupListAdapter;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupWindowHelper;
import com.wushi.lenovo.asia5b.app_utils.helper.CacheHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/1/15.
 * 网银充值
 */
public class FpxFragment extends BaseMvpFragment<FpxFragmentView, FpxFragmentPresenter>
        implements FpxFragmentView {
    @BindView(R.id.tv_fpx_notice)
    public TextView tv_fpx_notice;
    @BindView(R.id.tv_fpx_spinner)
    public TextView tv_fpx_spinner;
    @BindView(R.id.tv_fpx_amount_error)
    public TextView tv_fpx_amount_error;
    @BindView(R.id.tv_fpx_bank_error)
    public TextView tv_fpx_bank_error;
    @BindView(R.id.et_fpx_amount)
    public EditText et_fpx_amount;
    @BindView(R.id.bt_fpx_submit)
    public Button bt_fpx_submit;
    @BindView(R.id.ll_fpx_price_vessel)
    public LinearLayout ll_fpx_price_vessel;

    public PopupListAdapter<FpxBankBean> bankAdapter;
    public ListPopupWindow popupWindowBank;
    public ThirdTopupType thirdTopupType = ThirdTopupType.thirdtopup_by_fpx;

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_fpx;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setView();
        initView();
        initEvent();
    }

    @Override
    protected void lazyFetchData() {
        initData();
    }

    protected void setView() {
        //编辑充值金额
        //设置最低充值额度
        String fpx_minimum_value = CacheHelper.loadCreditMinByLocal().getFpx_minimum_value();
        et_fpx_amount.setHint(String.format(getString(R.string.topup_notice_min), fpx_minimum_value));

        //超链接
        EyeUtils.setLinkText(getContext(), getString(R.string.topup_notice), getString(R.string.termsconditions),
                tv_fpx_notice, WebUrlType.termsconditions_fpx);
    }

    @Override
    protected void initView() {

        bankAdapter = new PopupListAdapter<>(getContext(), R.layout.item_popupitem_withdraw_adapter);
        popupWindowBank = PopupWindowHelper.getListPopupWindow(getContext(), bankAdapter
                , tv_fpx_spinner, ListPopupWindow.WRAP_CONTENT);

    }


    @Override
    protected void initData() {
    }

    /**
     * 获取最低充值额度
     *
     * @return
     */
    protected float getAmount() {
        return StringUtils.parseFloat(et_fpx_amount.getText().toString().trim());
    }

    protected int getCheckout_log_id(){
        return 0;
    }

    protected TopupInfoBean getTopupInfoBean() {
        return null;
    }

    protected void submit() {
        boolean b = false;
        float amount = getAmount();
        if (amount < CacheHelper.loadCreditMinByLocal().getFpx_minimum_valueF()) {
            tv_fpx_amount_error.setVisibility(View.VISIBLE);
            b = true;
        }
        FpxBankBean selectItem = bankAdapter.getSelectItem();
        if (selectItem == null) {
            b = true;
            tv_fpx_bank_error.setVisibility(View.VISIBLE);
        }
        if (b) {
            return;
        }

        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", UserInfo.getMemberId());
        if (thirdTopupType == ThirdTopupType.thirdtopup_by_fpx) {//用网银充值我的账户
            map.put("amount", amount);
            map.put("other_amount", amount);
            map.put("bank", selectItem.getBankNum());
        } else if (thirdTopupType == ThirdTopupType.thirdtopup_from_orderpayment_ins_by_fpx) {//订单付款余额不足,用网银充值差额
            map.put("amount", amount);
            map.put("other_amount", amount);
            map.put("checkout_log_id", getCheckout_log_id());
            map.put("bank", selectItem.getBankNum());
        } else {//订单付款用网银付款
            map.put("fpx_bank", selectItem.getBankNum());
            map.put("checkout_log_id", getCheckout_log_id());
        }
        getPresenter().validateTopup(map, thirdTopupType);
    }

    @Override
    protected void initEvent() {
        popupWindowBank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popupWindowBank.dismiss();
                bankAdapter.setSelectPosition(position);
                tv_fpx_spinner.setText(bankAdapter.getSelectItemText());
                tv_fpx_bank_error.setVisibility(View.GONE);
            }
        });
        et_fpx_amount.addTextChangedListener(new BaseTextWatcher() {
            @Override
            public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
                tv_fpx_amount_error.setVisibility(View.GONE);
            }
        });
    }

    @OnClick({
            R.id.tv_fpx_spinner,
            R.id.bt_fpx_submit
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_fpx_spinner:
                List<FpxBankBean> bankModels = bankAdapter.getmDatas();
                if (bankModels == null || bankModels.size() == 0) {
                    getPresenter().getBanks2();
                }
                PopupWindowHelper.showPopupWindow(popupWindowBank);
                break;
            case R.id.bt_fpx_submit:
                submit();
                break;
            default:
                break;
        }
    }

    @Override
    public void validateTopupResult(ResultModel resultModel, Map<String, Object> map) {
        if (resultModel.isSuccess()) {

            if (thirdTopupType == ThirdTopupType.thirdtopup_by_fpx) {//用网银充值我的账户
            } else if (thirdTopupType == ThirdTopupType.thirdtopup_from_orderpayment_ins_by_fpx) {//订单付款余额不足,用网银充值差额
                map.put("fpx_amount", map.get("amount"));
                map.put("fpx_bank", map.get("bank"));
                map.remove("amount");
                map.remove("other_amount");
                map.remove("bank");
            } else {//订单付款用网银付款
            }

            Intent intent = new Intent(getContext(), TopupThirdActivity.class);
            intent.putExtra("map", (Serializable) map);
            intent.putExtra(CommonConstant.TOPUP_THIRDTOPUPTYPE, thirdTopupType);
            if (getTopupInfoBean() != null) {
                intent.putExtra(CommonConstant.TopupInfoBean, getTopupInfoBean());
            }
            startActivity(intent);
        }
    }

    @Override
    public void getbanks2Result(ResultModel<Map<String, String>> map) {
        try {
            Map<String, String> bankMap = map.getData();
            List<FpxBankBean> bankModelList = new ArrayList<>();
            bankModelList.clear();
            Iterator<Map.Entry<String, String>> iterator = bankMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                FpxBankBean fpxBankBean = new FpxBankBean();
                fpxBankBean.setBankNum(next.getKey());//设置银行卡号
                fpxBankBean.setBankName(next.getValue());//设置银行卡名称
                bankModelList.add(fpxBankBean);
            }
            bankAdapter.setDatas(bankModelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
